'use strict';

const Controller = require('egg').Controller;

class aduserController extends Controller {
    // 获取用户列表
    async userlist() {
        const { ctx } = this;
        let res = { "code": 0, "msg": "数据获取成功", "count": 0, "data": [] };
        //分页用到的页数及每页显示条数
        const page = ctx.request.query.page / 1 ? ctx.request.query.page / 1 : 1;
        const limit = ctx.request.query.limit / 1 ? ctx.request.query.limit / 1 : 20;
        //到数据表里面去验证数据的正确性
        const results = await this.app.mysql.select('users', {
            where: { status: 1 }, // WHERE 条件  
            columns: ['uid', 'username', 'gender', 'avater', 'status', 'score'], // 要查询的表字段
            orders: [['uid', 'asc']], // 排序方式
            limit: limit, // 返回数据量
            offset: (page - 1) * limit, // 数据偏移量
        });
        res.data = results;
        //查询满足条件的记录数
        const sql = `SELECT count(uid) as nums FROM users WHERE status = 1 `;
        const r = await this.app.mysql.query(sql);
        console.log(r); //获取满足条件的数据
        res.count = r[0].nums;
        ctx.body = res;
    }
}

module.exports = aduserController;